package com.bdf.ipharma.adapters;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JTable;

import com.bdf.ipharma.config.Session;
import com.bdf.ipharma.starter.McdPrs;


public class JTableButtonMouseListener extends MouseAdapter {
	private final JTable table;
	private Session session;
	
	
	
	public JTableButtonMouseListener(JTable table,Session session) {
		this.table = table;
		this.session =  session;
	}
	

	public void mouseClicked(MouseEvent e) {
		int column = table.getColumnModel().getColumnIndexAtX(e.getX());
		int row    = e.getY()/table.getRowHeight(); 

		if (row < table.getRowCount() && row >= 0 && column < table.getColumnCount() && column >= 0) {
		    Object value = table.getValueAt(row, column);
		    if (value instanceof JButton) {
		    	JButton button = (JButton)value;
		    	boolean enabled = (Boolean) session.get(McdPrs.COMP_ENABLED);
		    	if(enabled){
		    		button.doClick();
		    	}
		    }
		}
	}
}